package com.ckd.flyingtrip.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.ckd.flyingtrip.R;
import com.ckd.flyingtrip.adapter.HomeNewsAdapter;
import com.ckd.flyingtrip.constants.Constants;
import com.ckd.flyingtrip.javabean.HomeNewsBean;
import com.ckd.flyingtrip.utils.BcUtils;
import com.ckd.flyingtrip.utils.DataCheck;
import com.ckd.flyingtrip.utils.ItemDecoration;
import com.ckd.flyingtrip.utils.VolleyErrorUtils;
import com.ckd.flyingtrip.widge.SingleVolleyRequestQueue;
import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class HomeNewsActivity extends BaseActivity {


    @BindView(R.id.toolber_color_title)
    TextView title;
    @BindView(R.id.activity_home_news_recyclerView)
    RecyclerView recyclerView;

    private List<HomeNewsBean.DataBean> dataBeans = new ArrayList<>();
    private HomeNewsAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home_news);
        ButterKnife.bind(this);

        initView();
        initData();
        initListener();

    }

    private void initListener() {
        adapter.setOnItemClickListener(new HomeNewsAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Intent intent = new Intent(HomeNewsActivity.this, HomeNewsDetailActivity.class);
                intent.putExtra("title", dataBeans.get(position).getNoticeTitle());
                intent.putExtra("text", dataBeans.get(position).getNoticeMsg());
                startActivity(intent);
            }
        });
    }

    private void initData() {
        StringRequest request = new StringRequest(Request.Method.POST, Constants.getmessage, new Response.Listener<String>() {
            @Override
            public void onResponse(String s) {
                Log.i("消息", s);
                try {
                    JSONObject json = new JSONObject(s);
                    String code = json.getString("code");
                    String msg = json.getString("msg");
                    if (code.equals("1")) {
                        HomeNewsBean homeNewsBean = new Gson().fromJson(s, HomeNewsBean.class);
                        dataBeans.clear();
                        dataBeans.addAll(homeNewsBean.getData());
                        if (adapter != null)
                            adapter.notifyDataSetChanged();
                    } else
                        toast(msg);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                VolleyErrorUtils.checkError(volleyError);
            }
        }) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {

                HashMap<String, String> map = new HashMap<String, String>();
                JSONObject json_url = new JSONObject();
                try {
                    json_url.put("userId", BcUtils.getUid());
                    json_url.put("userArea", Constants.CITYID);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                map.put("inputParameter", DataCheck.signCheck(json_url.toString()));

                return map;
            }
        };
        SingleVolleyRequestQueue.getInstance(this).addToRequestQueue(request);
    }

    private void initView() {
        title.setText("消息");

        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.addItemDecoration(new ItemDecoration(0, 0, 0, 30));
        adapter = new HomeNewsAdapter(this, dataBeans);
        recyclerView.setAdapter(adapter);

    }

    @OnClick(R.id.toolber_color_back)
    public void onViewClicked() {
        finish();
    }
}
